package com.example.demo;
 
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
 
 
/**
 * Created by zhouhs on 2017/1/9.
 */
public class WordExportController3 {
	
	private static final Integer F_WIDTH = 400;
	
	private static final Integer S_WIDTH = 4000;
	
	private static final Integer HEIGHT = 600;
 
    public static void main(String[] args)throws Exception {
        //Blank Document
        XWPFDocument document= new XWPFDocument();
 
        //Write the Document in file system
        FileOutputStream out = new FileOutputStream(new File("create_table.docx"));
 
 
        //添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
 
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText("Java PoI");
        titleParagraphRun.setColor("000000");
        titleParagraphRun.setFontSize(20);
 
 
        //段落
        XWPFParagraph firstParagraph = document.createParagraph();
        XWPFRun run = firstParagraph.createRun();
        run.setText("Java POI 生成word文件。");
        run.setColor("696969");
        run.setFontSize(16);
 
        //设置段落背景颜色
        CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
        cTShd.setVal(STShd.CLEAR);
        cTShd.setFill("97FFFF");
 
 
        //换行
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun paragraphRun = paragraph.createRun();
        paragraphRun.setText("\r");
 
 
        //基本信息表格
        XWPFTable infoTable = document.createTable();
        //去表格边框
        infoTable.getCTTbl().getTblPr().unsetTblBorders();
 
 
        //列宽自动分割
        CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
        infoTableWidth.setType(STTblWidth.DXA);
        infoTableWidth.setW(BigInteger.valueOf(9072));
 
 
        //表格第一行
        XWPFTableRow infoTableRowOne = infoTable.getRow(0);
        infoTableRowOne.getCell(0).setText("职位");
        infoTableRowOne.addNewTableCell().setText(": Java 开发工程师");
 
        //表格第二行
        XWPFTableRow infoTableRowTwo = infoTable.createRow();
        infoTableRowTwo.getCell(0).setText("姓名");
        infoTableRowTwo.getCell(1).setText(": seawater");
 
        //表格第三行
        XWPFTableRow infoTableRowThree = infoTable.createRow();
        infoTableRowThree.getCell(0).setText("生日");
        infoTableRowThree.getCell(1).setText(": xxx-xx-xx");
 
        //表格第四行
        XWPFTableRow infoTableRowFour = infoTable.createRow();
        infoTableRowFour.getCell(0).setText("性别");
        infoTableRowFour.getCell(1).setText(": 男");
 
        //表格第五行
        XWPFTableRow infoTableRowFive = infoTable.createRow();
        infoTableRowFive.getCell(0).setText("现居地");
        infoTableRowFive.getCell(1).setText(": xx");
 
 
        //两个表格之间加个换行
        paragraph = document.createParagraph();
        paragraphRun = paragraph.createRun();
        paragraphRun.setText("\r");
 
        //工作经历表格
        XWPFTable ComTable = document.createTable();
 
 
        //列宽自动分割
        CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
        comTableWidth.setType(STTblWidth.AUTO);
        comTableWidth.setW(BigInteger.valueOf(9072));
 
        //表格第一行
        XWPFTableRow comTableRowOne = ComTable.getRow(0);
        comTableRowOne.getCell(0).setText("开始时间");
        comTableRowOne.addNewTableCell().setText("结束时间");
        comTableRowOne.addNewTableCell().setText("公司名称");
        comTableRowOne.addNewTableCell().setText("title");
 
        //表格第二行
        XWPFTableRow comTableRowTwo = ComTable.createRow();
        comTableRowTwo.getCell(0).setText("2016-09-06");
        comTableRowTwo.getCell(1).setText("至今");
        comTableRowTwo.getCell(2).setText("seawater");
        comTableRowTwo.getCell(3).setText("Java开发工程师");
 
        //表格第三行
        XWPFTableRow comTableRowThree = ComTable.createRow();
        comTableRowThree.getCell(0).setText("2016-09-06");
        comTableRowThree.getCell(1).setText("至今");
        comTableRowThree.getCell(2).setText("seawater");
        comTableRowThree.getCell(3).setText("Java开发工程师");
        
        //两个表格之间加个换行
        paragraph = document.createParagraph();
        paragraphRun = paragraph.createRun();
        paragraphRun.setText("\r");
        
        //测试表格
        XWPFTable testTable = document.createTable();
        //列宽自动分割
        comTableWidth = testTable.getCTTbl().addNewTblPr().addNewTblW();
        comTableWidth.setType(STTblWidth.DXA);
        comTableWidth.setW(BigInteger.valueOf(9072));
        
        //表格第一行
        comTableRowTwo = testTable.getRow(0);
        comTableRowTwo.getCell(0).setText("火灾自动报警系统");
        comTableRowTwo.addNewTableCell().setText("");
        comTableRowTwo.addNewTableCell().setText("面板各功能表显示输入输出功能");
        comTableRowTwo.addNewTableCell().setText("");
        comTableRowTwo.addNewTableCell().setText("");
        comTableRowTwo.addNewTableCell().setText("");
        comTableRowTwo.addNewTableCell().setText("");
        setCellWidth(comTableRowTwo.getCell(2), S_WIDTH);
        setCellWidth(comTableRowTwo.getCell(0), F_WIDTH);
        setCellCenter(comTableRowTwo.getCell(0));
        // setRowHeight(comTableRowTwo);
        
        comTableRowTwo = testTable.createRow();
        comTableRowTwo.getCell(0).setText("火灾自动报警系统");
        comTableRowTwo.getCell(1).setText("");
        comTableRowTwo.getCell(2).setText("面板各功能键动作");
        comTableRowTwo.getCell(3).setText("");
        comTableRowTwo.getCell(4).setText("");
        comTableRowTwo.getCell(5).setText("");
        comTableRowTwo.getCell(6).setText("");
        setCellWidth(comTableRowTwo.getCell(2), S_WIDTH);
        setCellWidth(comTableRowTwo.getCell(0), F_WIDTH);
        setCellCenter(comTableRowTwo.getCell(0));
        // setRowHeight(comTableRowTwo);
        
        comTableRowTwo = testTable.createRow();
        comTableRowTwo.getCell(0).setText("火灾自动报警系统");
        comTableRowTwo.getCell(1).setText("");
        comTableRowTwo.getCell(2).setText("面板各功能键动作");
        comTableRowTwo.getCell(3).setText("");
        comTableRowTwo.getCell(4).setText("");
        comTableRowTwo.getCell(5).setText("");
        comTableRowTwo.getCell(6).setText("");
        setCellWidth(comTableRowTwo.getCell(2), S_WIDTH);
        setCellWidth(comTableRowTwo.getCell(0), F_WIDTH);
        setCellCenter(comTableRowTwo.getCell(0));
        // setRowHeight(comTableRowTwo);
        
        comTableRowTwo = testTable.createRow();
        comTableRowTwo.getCell(0).setText("火灾自动报警系统");
        comTableRowTwo.getCell(1).setText("");
        comTableRowTwo.getCell(2).setText("面板各功能键动作");
        comTableRowTwo.getCell(3).setText("");
        comTableRowTwo.getCell(4).setText("");
        comTableRowTwo.getCell(5).setText("");
        comTableRowTwo.getCell(6).setText("");
        setCellWidth(comTableRowTwo.getCell(2), S_WIDTH);
        setCellWidth(comTableRowTwo.getCell(0), F_WIDTH);
        setCellCenter(comTableRowTwo.getCell(0));
        // setRowHeight(comTableRowTwo);
        
        comTableRowTwo = testTable.createRow();
        comTableRowTwo.getCell(0).setText("火灾自动报警系统");
        comTableRowTwo.getCell(1).setText("");
        comTableRowTwo.getCell(2).setText("面板各功能键动作");
        comTableRowTwo.getCell(3).setText("");
        comTableRowTwo.getCell(4).setText("");
        comTableRowTwo.getCell(5).setText("");
        comTableRowTwo.getCell(6).setText("");
        setCellWidth(comTableRowTwo.getCell(2), S_WIDTH);
        setCellWidth(comTableRowTwo.getCell(0), F_WIDTH);
        setCellCenter(comTableRowTwo.getCell(0));
        // setRowHeight(comTableRowTwo);
        
        comTableRowTwo = testTable.createRow();
        comTableRowTwo.getCell(0).setText("火灾自动报警系统");
        comTableRowTwo.getCell(1).setText("");
        comTableRowTwo.getCell(2).setText("面板各功能键动作");
        comTableRowTwo.getCell(3).setText("");
        comTableRowTwo.getCell(4).setText("");
        comTableRowTwo.getCell(5).setText("");
        comTableRowTwo.getCell(6).setText("");
        setCellWidth(comTableRowTwo.getCell(2), S_WIDTH);
        setCellWidth(comTableRowTwo.getCell(0), F_WIDTH);
        setCellCenter(comTableRowTwo.getCell(0));
        // setRowHeight(comTableRowTwo);
        
        comTableRowTwo = testTable.createRow();
        comTableRowTwo.getCell(0).setText("火灾自动报警系统");
        comTableRowTwo.getCell(1).setText("");
        comTableRowTwo.getCell(2).setText("面板各功能键动作");
        comTableRowTwo.getCell(3).setText("");
        comTableRowTwo.getCell(4).setText("");
        comTableRowTwo.getCell(5).setText("");
        comTableRowTwo.getCell(6).setText("");
        setCellWidth(comTableRowTwo.getCell(2), S_WIDTH);
        setCellWidth(comTableRowTwo.getCell(0), F_WIDTH);
        setCellCenter(comTableRowTwo.getCell(0));
        // setRowHeight(comTableRowTwo);
        mergeCellsVertically(testTable, 0, 0, 6);
        /*CTTc cttc = comTableRowTwo.getCell(0).getCTTc();
        CTP ctp = cttc.getPList().get(0);
        CTPPr ctppr = ctp.getPPr();
        if (ctppr == null) {
            ctppr = ctp.addNewPPr();
        }
        CTJc ctjc = ctppr.getJc();
        if (ctjc == null) {
            ctjc = ctppr.addNewJc();
        }
        ctjc.setVal(STJc.CENTER); //水平居中
*/        
      //设置表格数据
        /*XWPFTable comTable = document.createTable();
        comTable.getCTTbl().getTblPr().unsetTblBorders();
        comTableWidth = comTable.getCTTbl().addNewTblPr().addNewTblW();
        comTableWidth.setType(STTblWidth.AUTO);
        comTableWidth.setW(BigInteger.valueOf(9072));
        XWPFTableRow row = comTable.getRow(0);
        row.setHeight(600);
        row.getCell(0).setText("火灾自动报警系统");
        row.getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(300));
        row.getCell(0).setColor("E5E8E9");
        row.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        row.createCell();
        row.getCell(1).setColor("E5E8E9");
        row.createCell();
        row.getCell(2).setColor("E5E8E9");
        row.createCell();
        row.getCell(3).setColor("E5E8E9");
        mergeCellsHorizontal(comTable, 0,0,3);*/
        
        CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
        XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
 
        //添加页眉
        CTP ctpHeader = CTP.Factory.newInstance();
        CTR ctrHeader = ctpHeader.addNewR();
        CTText ctHeader = ctrHeader.addNewT();
        String headerText = "Java POI create MS word file.";
        ctHeader.setStringValue(headerText);
        XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
        //设置为右对齐
        headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
        XWPFParagraph[] parsHeader = new XWPFParagraph[1];
        parsHeader[0] = headerParagraph;
        policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
 
 
        //添加页脚
        CTP ctpFooter = CTP.Factory.newInstance();
        CTR ctrFooter = ctpFooter.addNewR();
        CTText ctFooter = ctrFooter.addNewT();
        String footerText = "http://blog.csdn.net/zhouseawater";
        ctFooter.setStringValue(footerText);
        XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
        headerParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFParagraph[] parsFooter = new XWPFParagraph[1];
        parsFooter[0] = footerParagraph;
        policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
 
 
        document.write(out);
        out.close();
        System.out.println("create_table document written success.");
    }
    
    // word跨列合并单元格
 	public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {  
         for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {  
             XWPFTableCell cell = table.getRow(row).getCell(cellIndex);  
             if ( cellIndex == fromCell ) {  
                 // The first merged cell is set with RESTART merge value  
                 cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);  
             } else {  
                 // Cells which join (merge) the first one, are set with CONTINUE  
                 cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);  
             }  
         }  
     }  
 	// word跨行并单元格
 	public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {  
         for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {  
             XWPFTableCell cell = table.getRow(rowIndex).getCell(col);  
             if ( rowIndex == fromRow ) {  
                 // The first merged cell is set with RESTART merge value  
                 cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);  
             } else {  
                 // Cells which join (merge) the first one, are set with CONTINUE  
                 cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);  
             }  
         }  
     }
 	
 	// 设置单元格垂直水平居中
 	public static void setCellCenter(XWPFTableCell cell) {
        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //垂直居中
        CTTc cttc = cell.getCTTc();
        CTTcPr ctPr = cttc.addNewTcPr();
        ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
        cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
 	}
 	
 	// 设置单元格宽度
  	public static void setCellWidth(XWPFTableCell cell,Integer width) {
  		cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(width));
  	}
  	
  	// 设置行高度
  	public static void setRowHeight(XWPFTableRow row,Integer height) {
  		row.setHeight(height);
  	}
  	
  	public static List<String> getData() {
  		List<String> list = new LinkedList<>();
  		list.add("面板各功能表显示输入输出功能");
  		list.add("面板各功能键动作");
  		list.add("报警、输出回路、断线故障声光显示功能");
  		list.add("备用电源断电、故障声光显示功能");
  		list.add("主音箱、地区音响、故障报警隔断功能");
  		list.add("主机报警、显示联动列印功能");
  		list.add("主机联动隔断功能");
  		list.add("主机与手动按钮通话功能");
  		list.add("主机报警输出功能");
  		list.add("各楼层温感探测器及信号回路");
  		list.add("各楼层煤气探测器及信号回路");
  		list.add("各楼层烟感探测器及信号回路");
  		list.add("各楼层手动按钮报警器及信号回路");
  		list.add("各楼层警铃及信号回路");
  		list.add("各联动模组及控制回路");
  		return list;
  	}
 
}